import { StorageManager } from '@vben-core/shared/cache';

import { KeyEnum } from './apiKeys';
import difyInstance from './instance';

const AppOptions = import.meta.env;
const apiUrl = AppOptions.VITE_DIFY_IP;
const storageManager = new StorageManager({
  prefix: '',
  storageType: 'sessionStorage',
});
// document
// 创建文档接口
const createDocument = (dataset_id: string, text: string) => {
  difyInstance.defaults.headers.common.Authorization = `Bearer ${KeyEnum.DATASET}`;

  // 固定参数
  const indexingTechnique = 'high_quality';
  const processRule = {
    mode: 'custom',
    rules: {
      pre_processing_rules: [
        {
          id: 'remove_extra_spaces',
          enabled: true,
        },
        {
          id: 'remove_urls_emails',
          enabled: false,
        },
      ],
      segmentation: {
        separator: '##',
        max_tokens: 500,
      },
    },
  };

  return difyInstance.post(
    `${apiUrl}/datasets/${dataset_id}/document/create_by_text`,
    {
      name: storageManager.getItem('userId'),
      text,
      indexing_technique: indexingTechnique,
      process_rule: processRule,
    },
  );
};

export default {
  createDocument,
};
